package com.itboye.pondteam;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.itboye.pondteam.app.MyApplication;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.language.LanguageSettingUtil;
import com.itboye.pondteam.pondteam.LoginActivity;
import com.itboye.pondteam.pondteam.MyDeviceActivity;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.SPUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;


/**
 * 日志名  格式   android_用户ID_yyyy-MM-dd
 *
 * @author young
 */
public class SplashActivity extends BaseActivity {

    Handler handler;
    boolean hasLogined;
    private MyApplication myApplication;
    private LanguageSettingUtil languageSetting;
    boolean isFirstInstall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setStatusBarColor(R.color.gray_eee);

        myApplication = (MyApplication) getApplication();
        languageSetting = myApplication.getLanguageSetting();// 检查是否已经初始化
        try {
            isFirstInstall = (boolean) SPUtils.get(this, null, Const.IS_FIRST, true);
            hasLogined = (boolean) SPUtils.get(this, null, Const.IS_LOGINED, false);
        } catch (Exception e) {

        }
    }

    private String paserTime(long milliseconds) {
        System.setProperty("user.timezone", "Asia/Shanghai");
        TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
        TimeZone.setDefault(tz);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String times = format.format(new Date(milliseconds));
        return times;
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        if (isFirstInstall) {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle(getString(R.string.wenxin));
            alert.setMessage(getString(R.string.xieyi));
            alert.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SPUtils.put(SplashActivity.this, null, Const.IS_FIRST, false);
                    startTimer();
                }
            });
            alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SPUtils.put(SplashActivity.this, null, Const.IS_FIRST, true);
                    finish();
                }
            });

            alert.create();
            alert.show();
        } else {
            startTimer();
        }

    }

    private void startTimer() {
        handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                if (!hasLogined) {
                    startActivity(new Intent(getApplicationContext(), LoginActivity.class));
                } else {
                    startActivity(new Intent(getApplicationContext(), MyDeviceActivity.class));
                }
                finish();
            }

            ;
        };
        handler.sendEmptyMessageDelayed(123, 2500);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (handler != null) {
            handler.removeMessages(123);
        }
        finish();
    }

    @Override
    public void finish() {
        // TODO Auto-generated method stub
        super.finish();
//		overridePendingTransition(0, R.anim.anim_falldown);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}
